package com.csu54sher.basereservation.util;

import com.csu54sher.basereservation.constant.BcryptConstant;
import jakarta.validation.constraints.NotBlank;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * BCrypt 工具类
 * <p>仅用于密码加密（哈希），不做验证逻辑</p>
 *
 * @author liuke
 */
public class BcryptUtil {

    // 线程安全，可复用
    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BcryptConstant.DIGEST_STRENGTH);

    /**
     * 使用 BCrypt 加密明文密码
     *
     * @param rawPassword 明文密码
     * @return 加密后的哈希字符串（包含随机盐）
     * @author liuke
     */
    public static String encrypt(String rawPassword) {
        if (rawPassword == null || rawPassword.isEmpty()) {
            throw new IllegalArgumentException("密码不能为空");
        }
        return encoder.encode(rawPassword);
    }


    public static boolean checkPassword(String plainPassword, String hashedPassword) {
        return encoder.matches(plainPassword, hashedPassword);
    }
}
